package com.fafu.service.Impl;

import com.fafu.dao.mapper.ImageMapper;
import com.fafu.dao.mapper.StudentMapper;
import com.fafu.dao.mapper.StudentrepairMapper;
import com.fafu.model.Image;
import com.fafu.model.Student;
import com.fafu.model.Studentrepair;
import com.fafu.service.StudentRepairService;
import com.fafu.service.StudentService;
import com.github.fge.jackson.JsonNodeReader;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;
import tk.mybatis.mapper.entity.Example;
import util.Dictionary;
import util.JsonRsp;
import util.MD5Utils;
import util.MyMap;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentrepairMapper studentrepairMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Override
    public Map<String,Object> getStudent(String studentIden) {
        Example example = new Example(Student.class);
        example.and().andEqualTo("studentIden", studentIden);
        List<Student> studentList = studentMapper.selectByExample(example);
        if(studentIden == null || studentList.size() == 0)
            return new MyMap().put("code",Dictionary.CODE.IDEN_ERROR).put("msg","学号不存在")
                    .toMap();
        studentList.get(0).setPassword(null);
        studentList.get(0).setSalt(null);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS)
                .put("data",studentList.get(0)).toMap();
    }

    @Override
    public Map<String, Object> getStudentById(Integer id) {
        Student  student= studentMapper.selectByPrimaryKey(id);
        student.setPassword(null);
        student.setSalt(null);
        return new MyMap().put("data",student).put("code", Dictionary.CODE.SUCCESS).toMap();
    }

    @Override
    public Map<String, Object> getStudentList(Map<String, Object> paramMap) {
        PageHelper.startPage((Integer)paramMap.get("pageNum"),(Integer)paramMap.get("pageSize"));
        List list = studentMapper.fuzzySearch(paramMap);
        PageInfo pageInfo = new PageInfo(list);
        List<Student> slist = pageInfo.getList();
        for(Student s:slist){
            s.setPassword(null);
            s.setSalt(null);
        }
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).put("data",slist).put("count",pageInfo.getTotal()).toMap();
    }

    @Override
    public Map<String, Object> stateChange(Map<String, Object> paramMap) {
        Student student = studentMapper.selectByPrimaryKey(paramMap.get("id"));
        if(student == null)
            return new MyMap().put("code", Dictionary.CODE.RECORD_NOT_EXITS).put("msg","record not exist").toMap();
        student.setIsUsed( (student.getIsUsed() +1)%2 );
        studentMapper.updateByPrimaryKeySelective(student);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).toMap();
    }

    @Override
    public Map<String, Object> login(Map<String, Object> paramMap) {
        String iden = (String) paramMap.get("studentId");
        String pwd = (String) paramMap.get("password");
        Example example = new Example(Student.class);
        example.and().andEqualTo("studentIden",iden);
        List<Student> students = studentMapper.selectByExample(example);
        if(students == null || students.size() == 0)
            return new MyMap().put("code", Dictionary.CODE.IDEN_ERROR).put("msg","账号不存在").toMap();
        if(!students.get(0).getPassword().equals(MD5Utils.passwordAddSalt(pwd,students.get(0).getSalt())))
            return new MyMap().put("code", Dictionary.CODE.PASSWORD_ERROR).put("msg","密码错误").toMap();
        Example imgExample = new Example(Image.class);
        imgExample.and().andEqualTo("tableIden", Dictionary.TABLE.TABLE_USER_PIC);
        imgExample.and().andEqualTo("repairid",students.get(0).getId());
        List<Image> images = imageMapper.selectByExample(imgExample);
        String imgurl="";
        if(images==null || images.size() == 0)
            imgurl = "null";
        else imgurl = images.get(0).getUrl();
        return new MyMap().put("code", Dictionary.CODE.SUCCESS)
                .put("msg","success")
                .put("studentIden",students.get(0).getStudentIden())
                .put("id",students.get(0).getId())
                .put("isused",students.get(0).getIsUsed())
                .put("imgurl",imgurl)
                .put("name",students.get(0).getStudengName())
                .put("area",students.get(0).getArea()).toMap();
    }

    @Override
    public Map<String, Object> changePwd(Map<String, Object> paramMap) {
        String iden = (String) paramMap.get("studentId");
        String oldpwd = (String) paramMap.get("oldpwd");
        String pwd = (String) paramMap.get("newpwd");
        Example example = new Example(Student.class);
        example.and().andEqualTo("studentIden",iden);
        List<Student> students = studentMapper.selectByExample(example);
        if(students == null || students.size() == 0)//判断账号
            return new MyMap().put("code", Dictionary.CODE.IDEN_ERROR).put("msg","账号不存在").toMap();
        if(!students.get(0).getPassword().equals(MD5Utils.passwordAddSalt(oldpwd,students.get(0).getSalt())))//判断密码
            return new MyMap().put("code", Dictionary.CODE.PASSWORD_ERROR).put("msg","密码错误").toMap();
        String salt = MD5Utils.createSalt();
        String pwdSalt = MD5Utils.passwordAddSalt(pwd,salt);
        Student student = students.get(0);
        student.setPassword(pwdSalt);
        student.setSalt(salt);
        studentMapper.updateByPrimaryKeySelective(student);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).put("msg","密码修改成功").toMap();
    }

    @Override
    public Map<String, Object> msgChange(Map<String, Object> paramMap) {
        Integer id = (Integer) paramMap.get("reid");
        String msg = (String) paramMap.get("msg");
        Studentrepair studentrepair = studentrepairMapper.selectByPrimaryKey(id);
        if(!studentrepair.getState().equals(Dictionary.REPAIR.REPAIR_STATE_NO_READ))
            return new MyMap().put("code",100).put("msg","操作失败").toMap();
        studentrepair.setMsg(msg);
        studentrepairMapper.updateByPrimaryKeySelective(studentrepair);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).put("msg","修改成功").toMap();
    }
}
